package server;

/**
 * Class to help developers access the database if they can't use sql.
 * @author Terry
 */
import iface.HvZUser;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.Scanner;

public class DatabaseAdminMain {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        DBControlPanel cp = new DBControlPanel("root", "bitnami");

        while (true) {

            System.out.println("Menu:");
            System.out.println("-----");

            System.out.println("1. Show all users");
            System.out.println("2. Does user exist");
            System.out.println("3. Update user's zombie status");
            System.out.println("4. Delete user from table");
            System.out.println("5. Delete all users from table");
            System.out
                    .println("6. Change the database. Default = account_db | Test server = test_account_db");

            System.out.println("0. Quit");

            int val = scan.nextInt();
            try {
                switch (val) {
                case 0:
                    System.out.println("Goodbye");
                    return;
                case 1:
                    System.out.println("username,phone,zombie");
                    Iterator<HvZUser> iter = cp.getAllUsers().iterator();
                    while (iter.hasNext())
                        System.out.println(iter.next().toString());
                    System.out.println();
                    break;
                case 2:
                    System.out
                            .println("Please enter the username to search for");
                    String search = scan.next();
                    System.out.println(cp.doesUserExist(search));
                    break;
                case 3:
                    System.out.println("Enter the user to change");
                    String name = scan.next();
                    System.out
                            .println("Enter the type for the user to become.");
                    System.out
                            .println("0 for human.\t1 for zombie.\t2 for observer");
                    int type = scan.nextInt();
                    cp.updateUserStatus(name, type);
                    System.out.println();
                    break;
                case 4:
                    System.out.println("Enter user name:");
                    String un = scan.next();
                    cp.deleteUser(un);
                    System.out.println();
                    break;
                case 5:
                    System.out
                            .println("Are you absolutely sure?\n'y' to confirm");
                    String confirmation = scan.next();
                    if (confirmation.equals("y"))
                        cp.deleteAll();
                    System.out.println();
                    break;
                case 6:
                    System.out.println("Enter the DB name");
                    String db = scan.next();
                    cp.setDatabase(db);
                    break;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }
}
